/**
 * @author pokoyo
 */

	// 	$(function() {
	// 		$.jtabel();
	// 	});	

	var selectedPatientId;
	$(document).ready(function() {
		$.jtabel();
		//checkedCheck();

	});

	//   function checkedCheck() {
	//   	var value = $("input[name=search-type]:checked").val();

	//   	if (value == "patient") {
	//   		$('#srchExamine').hide();
	//   		$('#srchPatient').show('slow');
	//   		$('#labservice').hide();
	//   	} else {
	//   		$('#srchExamine').show('slow');
	//   		$('#srchPatient').hide();
	//   		$('#labservice').hide();
	//   	}
	//   }

	function getPaList(type) {
		if (type == 'id') {
			$.ajax({
				url : './patientaction.do?act=getbyid&id='
						+ $('input[name=idPatient]').val(),
				dataType : "text",
				timeout : 30000,
				statusCode : {
					404 : function() {
						alert('page not found');
					}
				},
				success : function(data) {
					var obj = $.parseJSON(data);
					$('#patient-list').html("");
					viewPaList(obj);
				}
			});
		} else {
			$.ajax({
				url : './patientaction.do?act=getbyname&firstname='
						+ $('input[name=firstName]').val() + '&lastname='
						+ $('input[name=lastName]').val(),
				dataType : "text",
				timeout : 30000,
				statusCode : {
					404 : function() {
						alert('page not found');
					}
				},
				success : function(data) {
					$('#patient-list').html("");
					var obj = $.parseJSON(data);
					if (!$.isEmptyObject(obj)) {
						for (key in obj) {
							var item = obj[key];
							viewPaList(item);
						}
					} else {
						viewPaList({});
					}

				}
			});
		}
	}

	function viewPaList(item) {
		var tr = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
		if (!$.isEmptyObject(item)) {
			var id = item['idPatientDetail'];
			tr = '<tr><td>' + id + '</td><td>' + item['firstName']
					+ '</td><td>' + item['lastName'] + '</td><td>'
					+ item['address'] + '</td><td>'
					+ "<a class='edit' onclick='listExa(" + id
					+ ")'>Select</a></td></tr>";
		}
		$('#patient-list').append(tr);
		$.jtabel();
	}

	function viewExaList(item) {
		var tr = '<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>';
		if (!$.isEmptyObject(item)) {
			var id = item['idExamine'];

			var dle = "";
			if (item['examineDay'] == null) {
				dle = "<a class='edit' onclick='deleteExa(" + id
						+ ")'>Delete</a>";
			}

			tr = '<tr><td>' + id + '</td><td>' + item['idPatient']
					+ '</td><td>' + item['appointmentDay'] + '</td><td>'
					+ item['examineDay'] + '</td><td>' + item['doctor']
					+ '</td><td>' + item['disease'] + '</td><td>' + dle
					+ "</td></tr>";
		}
		$('#examine-list').append(tr);
		$.jtabel();
	}

	function deleteExa(id) {
		$.ajax({
			url : './patientaction.do?act=deleteexamine&id=' + id,
			dataType : "text",
			timeout : 30000,
			statusCode : {
				404 : function() {
					alert('page not found');
				}
			},
			success : function(data) {
				if (data == 'success') {
					alert("scuceess");
					listExa(selectedPatientId);
				}else{
					alert(data);
				}
			}
		});
	}

	function listExa(id) {
		selectedPatientId = id;
		$.ajax({
			url : './patientaction.do?act=getexamine&id=' + id,
			dataType : "text",
			timeout : 30000,
			statusCode : {
				404 : function() {
					alert('page not found');
				}
			},
			success : function(data) {
				$('#examine-list').html("");
				var obj = $.parseJSON(data);
				if (obj.length > 0) {
					for (key in obj) {
						var item = obj[key];
						viewExaList(item);
					}
				} else {
					viewExaList({});
				}

			}
		});
	}